[root@openGauss155 auto_frame]# vim testcase/CBM/developer_ShareStorage_CBM_Case0008.py 
"""
Case Type   : CBM
Case Name   : 开启CBM参数后， 进行一定业务压力压力, 进行failover操作
Create At   : 2024-04-07
Owner       : songshixuan
Description :
    1、查看集群状态 cm_ctl query -Cv
    2、开启CBM参数，进行TPCC压力测试
    3、构建failover操作
    4、再次查看切换后集群状态 cm_ctl query -Cv
    5、重新启动原主节点
    6、恢复集群原始状态
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、参数开启成功，TPCC压力测试正常进行
    3、构建failover操作成功
    4、备节点上升为新主节点，原主节点stop
    5、主节点启动成功，以备节点启动
    6、恢复集群原始状态成功,集群状态正常
History     :
    1、2025-04-07 创建
"""

import os
import unittest
import time

from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro
from yat.test import Node

Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')
Standby2_SH = CommonSH('Standby2DbUser')
Primary_ROOTSH= CommonSH('PrimaryRoot')

@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')
        self.s2_comsh = CommonSH('Standby2DbUser')
        self.param = 'enable_cbm_tracking'
        self.com = Common()
        self.tpcc_user = Node('PrimaryRoot')

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:gs_guc set方式开启cbm 参数;  expect:修改成功-----'
        self.log.info(text)

        value = Primary_SH.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f"{self.param} = on")
        self.log.info(value)
        self.assertTrue(value, '修改失败')


        text = '-----step3:开启tpcc expect:运行成功，此处不做检查-----'
        self.log.info(text)
        start_tpcc = ComThread(self.com.start_tpcc,
                                 args=(self.tpcc_user,'/data/benchmarksql-5.0/run',
                                       './runBenchmark.sh props_write.pg'))
        start_tpcc.setDaemon(True)
        start_tpcc.start()
        status = Primary_ROOTSH.exec_cmd_under_root("ps -ux | grep './runBenchmark.sh' | grep -v -- '--color'")
        self.log.info(status)
        self.assertIn('props_write.pg', status, '执行失败' + text)
        time.sleep(10)

        text = '-----step4:原主节点构建; expect:停止成功，主节点进入Manually stopped状态-----'
        self.log.info(text)
        # 运行failover脚本进行failover构建
        stopdn = Primary_SH.exec_failover()
        self.log.info(stopdn)
        time.sleep(50)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        res1 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        res2 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        res3 = status.splitlines()[-1].split('|')[2].split('6003')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.log.info(res3)
        self.assertTrue('S Primary Normal' in res2 or 'S Standby Normal' in res2,
                '执行失败' + text)
        self.assertTrue('S Primary Normal' in res3 or 'S Standby Normal' in res3,
                '执行失败' + text)

        text = '-----step5:原节点进行节点启动; expect:执行成功-----'
        self.log.info(text)
        startdn = Primary_SH.exec_cm_ctl('start')
        self.log.info(startdn)
        self.assertIn(self.constant.cm_start_success_msg, startdn,
                '拉起dn节点失败')
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        res1 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.log.info(res1)
        self.assertIn('P Standby Normal', res1, '执行失败' + text)

    def tearDown(self):
        text = '-----step7:尝试进行stop集群操作; expect:集群停止成功-----'
        status = Primary_SH.exec_cm_ctl(mode='stop', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        text = '-----step8:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)

        recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                      '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                    status.splitlines()[-1].split('|')[2].split('6003')[-1],
                    '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')